﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Projektarbete
{
    enum MovementDirection : byte { none, up, down, left, right };

    static class Player
    {
        public enum Stats : byte { Strength, Agility };

        public static event VoidPointer EventMove;

        private static ushort health;
        public static string getName { get { return name; } }
        private static string name;
        public static ushort getHealth { get { return health; } }
        private static ushort mana;
        public static ushort getMana { get { return mana; } }

        public static int gold;

        private static float movementSpeed;

        public static Vector2 position;

        private static Texture2D spritesheet;

        private static Rectangle drawRectangle;

        private static LinkedList<Point> interactedTiles;
        private static LinkedList<MovementDirection> movementList;

        public static bool IsPlaying { get; set; } //TODO: används???
        private static bool isMoving;
        private static bool useMovementList;
        private static bool tempBool;

        private static MovementDirection movementDirection;
        private static MovementDirection facingDirection;
        public static MovementDirection getFacingDirection
        {
            get { return facingDirection; }
        }

        //keyLayout är i den här ordningen:
        /*
         * [0] = Gå uppåt
         * [1] = Gå neråt
         * [2] = Gå vänster
         * [3] = Gå höger
         * [4] = Interact
         * [5] = Modifyer key
         * [6] = Inventory
        */
        private static Keys[] keyLayout;

        //public Inventory equips;
        public static Inventory inventory;
        public static EquipInventory equipsInventory;

        public static Keys[] KeyLayout
        {
            set
            {
                keyLayout = value;
            }
        }

        private static byte animationIndex;
        private static byte animationRow;
        private static float animationTime;
        private static float maxAnimationTime;

        //STATS
        private static ushort strength;
        public static ushort Strength
        {
            get
            {
                return strength;
            }
        }

        private static ushort agility;
        public static ushort Agility
        {
            get
            {
                return agility;
            }
        }

        /// <summary>
        /// Create a new player
        /// </summary>
        /// <param name="Name">Name of the player</param>
        /// <param name="health">Health of the player</param>
        /// <param name="movementSpeed">Movement speed</param>
        /// <param name="position">Intitial position</param>
        /// <param name="spritesheet">Spritesheet to use</param>
        /// <param name="keyLayout">[0] = walk up, [1] = walk down, [2] = walk left, [3] = walk right, [4] = interact, [5] = modifyer key, [6] = inventory </param>
        public static void Initialize(string Name, ushort Health, float MovementSpeed, Vector2 Position, Texture2D Spritesheet, Keys[] KeyLayout)
        {
            name = Name;
            health = Health;
            spritesheet = Spritesheet;
            position = Position;
            movementSpeed = MovementSpeed;
            keyLayout = KeyLayout;

            animationIndex = 0;
            animationRow = 0;
            animationTime = 0;

            //TODO: CHANGE MOVEMENTSPEED
            switch ((int)movementSpeed)
            {
                case 0:
                    throw new Exception("Nope");
                case 1:
                case 2:
                    maxAnimationTime = 2;
                    break;
                case 3:
                    maxAnimationTime = 1;
                    break;
                default:
                    maxAnimationTime = 0.5f;
                    break;
            }

            inventory = new Inventory(100, 100);
            equipsInventory = new EquipInventory();

            isMoving = false;

            movementDirection = MovementDirection.none;
            facingDirection = MovementDirection.none;

            drawRectangle = new Rectangle(0, 0, 24, 24);

            interactedTiles = new LinkedList<Point>();
            movementList = new LinkedList<MovementDirection>();

            MapHandler.UpdateLightmap();
        }

        /// <summary>
        /// Update the player (to be called every frame/tick)
        /// </summary>
        /// <param name="Map">Send the map handler</param>
        public static void Update()
        {
            UpdateInput();
        }

        /// <summary>
        /// Hurts the player
        /// </summary>
        /// <param name="amount">amount to hurt the player</param>
        public static void HurtPlayer(ushort amount)
        {
            health -= amount;

            HUD.AddNotification(new Notification("You were hurt " + amount + " points of health!", NotificationColor.PlayerHurt, NotificationLifetime.PlayerHurt));
        }

        /// <summary>
        /// Heals the player
        /// </summary>
        /// <param name="amount">amount to heal the player</param>
        public static void HealPlayer(ushort amount)
        {
            health += amount;

            HUD.AddNotification(new Notification("You were healed " + amount + " points of health!", NotificationColor.PlayerHealed, NotificationLifetime.PlayerHealed));
        }

        public static void RemoveMana(ushort amount)
        {
            mana -= amount;

            HUD.AddNotification(new Notification("You lost " + amount + " mana!", NotificationColor.PlayerHurt, NotificationLifetime.PlayerHurt));
        }

        private static void RestoreMana(ushort amount)
        {
            mana += amount;

            HUD.AddNotification(new Notification("You gained " + amount + " mana!", NotificationColor.PlayerHealed, NotificationLifetime.PlayerHealed));
        }
        
        public static void IncreaseStat(Stats stat, int amount)
        {
            switch (stat)
            {
                case Stats.Strength:
                    strength += (ushort)amount;
                    break;
                case Stats.Agility:
                    agility += (ushort)amount;
                    break;
            }

            if (amount == 1)
            {
                HUD.AddNotification(new Notification(stat.ToString() + " increased", NotificationColor.Info, NotificationLifetime.Info));
            }
            else if(amount > 1)
            {
                HUD.AddNotification(new Notification(stat.ToString() + " increased by " + amount.ToString(), NotificationColor.Info, NotificationLifetime.Info));
            }
        }

        public static void DecreaseStat(Stats stat, int amount)
        {
            switch (stat)
            {
                case Stats.Strength:
                    strength -= (ushort)amount;
                    break;
                case Stats.Agility:
                    agility -= (ushort)amount;
                    break;
            }

            if (amount == 1)
            {
                HUD.AddNotification(new Notification(stat.ToString() + " decreased", NotificationColor.Info, NotificationLifetime.Info));
            }
            else if (amount > 1)
            {
                HUD.AddNotification(new Notification(stat.ToString() + " decreased by " + amount.ToString(), NotificationColor.Info, NotificationLifetime.Info));
            }
        }

        private static void Animate()
        {
            animationTime++;

            if (animationTime > maxAnimationTime)
            {
                animationTime = 0;
                animationIndex++;

                if (animationIndex > 7)
                    animationIndex = 0;

                drawRectangle.X = animationIndex * 24;
            }
        }

        private static void RoundAnimation()
        {
            if (animationIndex == 3 || animationIndex == 5)
                animationIndex = 4;
            else
                animationIndex = 0;

            drawRectangle.X = animationIndex * 24;
        }

        private static void UpdateInput()
        {
            //Rör sig spelaren?
            if (isMoving)
            {
                //Rör spelaren
                if (useMovementList)
                {
                    tempBool = false; //Har spelaren tryckt på en knapp?

                    for (int x = 0; x < 4; x++)
                    {
                        if (Input.IsKeyDown(keyLayout[x])) //Kolla igenom movement-knapparna
                        {
                            //Låt spelaren kontrolera karaktären om spelaren trycker på en knapp
                            if (position.X % 24 == 0 && position.Y % 24 == 0)
                            {
                                isMoving = false;
                                useMovementList = false;

                                movementList.Clear();

                                tempBool = true;
                            }
                            else
                            {
                                MovementDirection lastDir = movementDirection;

                                movementList.Clear(); 

                                movementList.AddFirst(lastDir);
                            }
                        }
                    }

                    if (Input.IsNewMouseOnePress())
                    {
                        if (position.X % 24 == 0 && position.Y % 24 == 0)
                        {
                            if (PathfindToMousePoint())
                            {
                                if (position.X % 24 != 0 || position.Y % 24 != 0)
                                {
                                    movementDirection = movementList.First();
                                }
                            }
                            else
                            {
                                movementList.Clear();

                                isMoving = false;
                                useMovementList = false;
                            }
                        }
                        else
                        {
                            MovementDirection lastDir = movementDirection;

                            if (PathfindToMousePoint())
                            {
                                if (position.X % 24 != 0 || position.Y % 24 != 0)
                                {
                                    movementList.AddFirst(lastDir);
                                    movementDirection = movementList.First();
                                }
                            }
                            else
                            {
                                movementList.Clear();

                                if (position.X % 24 != 0 || position.Y % 24 != 0)
                                    movementList.AddFirst(lastDir);
                            }
                        }

                        
                    }

                    if (!tempBool)
                        MoveFromMovementList();
                }
                else
                {
                    Move();
                }
            }
            else //Annars acceptera ny input
            {
                #region mouse input
                if (Input.IsNewMouseOnePress())
                {
                    if (!PopupManager.IsOverlayOpen())
                        PathfindToMousePoint();
                }
                #endregion

                #region keyboard input
                if (!Input.IsKeyDown(keyLayout[5]))
                {
                    if (Input.IsKeyDown(keyLayout[0]))
                    {
                        facingDirection = MovementDirection.up;
                        if (!Collision.TestCollision(MapHandler.map[(int)GetTiledPositionToPoint().Y - 1][(int)GetTiledPositionToPoint().X].getTextureID))
                        {
                            if (EventMove != null)
                                EventMove();
                            isMoving = true;
                            movementDirection = MovementDirection.up;
                        }
                    }
                    else if (Input.IsKeyDown(keyLayout[1]))
                    {
                        facingDirection = MovementDirection.down;
                        if (!Collision.TestCollision(MapHandler.map[(int)GetTiledPositionToPoint().Y + 1][(int)GetTiledPositionToPoint().X].getTextureID))
                        {
                            if (EventMove != null)
                                EventMove();
                            isMoving = true;
                            movementDirection = MovementDirection.down;
                        }
                    }
                    else if (Input.IsKeyDown(keyLayout[2]))
                    {
                        facingDirection = MovementDirection.left;
                        animationRow = 1;
                        drawRectangle.Y = animationRow * 24;
                        if (!Collision.TestCollision(MapHandler.map[(int)GetTiledPositionToPoint().Y][(int)GetTiledPositionToPoint().X - 1].getTextureID))
                        {
                            if (EventMove != null)
                                EventMove();
                            isMoving = true;
                            movementDirection = MovementDirection.left;
                        }
                    }
                    else if (Input.IsKeyDown(keyLayout[3]))
                    {
                        facingDirection = MovementDirection.right;
                        animationRow = 0;
                        drawRectangle.Y = animationRow * 24;
                        if (!Collision.TestCollision(MapHandler.map[(int)GetTiledPositionToPoint().Y][(int)GetTiledPositionToPoint().X + 1].getTextureID))
                        {
                            if (EventMove != null)
                                EventMove();
                            isMoving = true;
                            movementDirection = MovementDirection.right;
                        }
                    }
                }
                else
                {
                    if (Input.IsKeyDown(keyLayout[0]))
                    {
                        facingDirection = MovementDirection.up;
                        if (EventMove != null)
                            EventMove();
                    }
                    else if (Input.IsKeyDown(keyLayout[1]))
                    {
                        facingDirection = MovementDirection.down;
                        if (EventMove != null)
                            EventMove();
                    }
                    else if (Input.IsKeyDown(keyLayout[2]))
                    {
                        facingDirection = MovementDirection.left;
                        if (EventMove != null)
                            EventMove();
                    }
                    else if (Input.IsKeyDown(keyLayout[3]))
                    {
                        facingDirection = MovementDirection.right;
                        if (EventMove != null)
                            EventMove();
                    }
                }

                if (Input.IsNewPress(keyLayout[4])) //Interact
                {
                    if (EventMove != null)
                        EventMove();

                    if (InteractWithFacingTile())
                    {
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                    }
                    else
                    {
                        AIManager.InteractWithMerchant(GetFacingPositionAsVector2());
                    }
                }
                #endregion
            }
            if (Input.IsNewPress(keyLayout[6])) //Inventory
            {
                PopupManager.OpenCloseInventory();
            }
        }

        private static bool PathfindToMousePoint()
        {
            Point cameraScroll = Global.ToTilePosition(new Vector2(Camera.position.X, Camera.position.Y));

            Point toPoint = Global.ToTilePosition(new Vector2(Input.GetMousePosition().X, Input.GetMousePosition().Y - 12));

            toPoint.X -= 15 - (cameraScroll.X - 1);
            toPoint.Y -= 11 - (cameraScroll.Y - 1);

            //Fick ut alla värden ovan efter experimentation

            if ((toPoint.X > 0 && toPoint.X < MapHandler.mapDimensions.X - 1) &&
                (toPoint.Y > 0 && toPoint.Y < MapHandler.mapDimensions.Y - 1))
            {
                if (Global.CanPlayerSee(toPoint))
                {
                    movementList = Pathfinding.AStarWithVision(GetTiledPositionToPoint(), toPoint);

                    if (movementList.Count > 0)
                    {
                        movementDirection = movementList.First();
                        facingDirection = movementDirection;

                        if (movementDirection == MovementDirection.right)
                        {
                            animationRow = 0;
                            drawRectangle.Y = animationRow * 24;
                        }
                        else if (movementDirection == MovementDirection.left)
                        {
                            animationRow = 1;
                            drawRectangle.Y = animationRow * 24;
                        }

                        useMovementList = true;
                        isMoving = true;
                        return true;
                    }
                    else
                    {
                        HUD.AddNotification(new Notification("Cannot see"));
                    }
                }
                else
                {
                    HUD.AddNotification(new Notification("Cannot see"));
                }
            }
            else
            {
                HUD.AddNotification(new Notification("Cannot go there"));
            }

            return false;
        }

        /// <summary>
        /// Moves the player in the direction defined in this class
        /// </summary>
        private static void Move()
        {
            switch (movementDirection)
            {
                case MovementDirection.up:
                    position.Y -= movementSpeed;
                    Animate();

                    if (position.Y % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.down:
                    position.Y += movementSpeed;
                    Animate();

                    if (position.Y % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.left:
                    position.X -= movementSpeed;
                    Animate();

                    if (position.X % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.right:
                    position.X += movementSpeed;

                    Animate();

                    if (position.X % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Moves the player in the given direction
        /// </summary>
        private static void MoveFromMovementList()
        {
            facingDirection = movementDirection;
            switch (movementDirection)
            {
                case MovementDirection.up:
                    position.Y -= movementSpeed;
                    Animate();

                    if (position.Y % 24 == 0)
                    {
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();

                        movementList.RemoveFirst();

                        if (movementList.Count > 0)
                        {
                            movementDirection = movementList.First();
                        }
                        else
                        {
                            isMoving = false;
                            useMovementList = false;
                        }
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.down:
                    position.Y += movementSpeed;
                    Animate();

                    if (position.Y % 24 == 0)
                    {
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();

                        movementList.RemoveFirst();

                        if (movementList.Count > 0)
                        {
                            movementDirection = movementList.First();
                        }
                        else
                        {
                            isMoving = false;
                            useMovementList = false;
                        }
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.left:
                    animationRow = 1;
                    drawRectangle.Y = animationRow * 24;

                    position.X -= movementSpeed;
                    Animate();

                    if (position.X % 24 == 0)
                    {
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();

                        movementList.RemoveFirst();

                        if (movementList.Count > 0)
                        {
                            movementDirection = movementList.First();
                        }
                        else
                        {
                            isMoving = false;
                            useMovementList = false;
                        }
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                case MovementDirection.right:
                    animationRow = 0;
                    drawRectangle.Y = animationRow * 24;

                    position.X += movementSpeed;
                    Animate();

                    if (position.X % 24 == 0)
                    {
                        MapHandler.UpdateLightmap();
                        HUD.RefreshMinimap(GetTiledPositionToPoint());
                        RoundAnimation();

                        movementList.RemoveFirst();

                        if (movementList.Count > 0)
                        {
                            movementDirection = movementList.First();
                        }
                        else
                        {
                            isMoving = false;
                            useMovementList = false;
                        }
                    }
                    Camera.UpdateMatrix(new Vector2(position.X + 12, position.Y + 12));
                    break;
                default:
                    isMoving = false;
                    useMovementList = false;
                    break;
            }
        }

        /// <summary>
        /// Get the tiled position of the player
        /// </summary>
        /// <returns>A Vector2 containing the tiled position</returns>
        public static Vector2 GetTiledPositionToVector2()
        {
            return new Vector2((float)Math.Floor(position.X / 24), (float)Math.Floor(position.Y / 24));
        }

        /// <summary>
        /// Get the tiled position of the player
        /// </summary>
        /// <returns>A Point containing the tiled position</returns>
        public static Point GetTiledPositionToPoint()
        {
            return new Point((int)Math.Floor(position.X / 24), (int)Math.Floor(position.Y / 24));
        }

        /// <summary>
        /// Interacts with the tile that the player is facing
        /// </summary>
        /// <param name="Map">the Map containing the current map</param>
        public static bool InteractWithFacingTile()
        {
            //NET-TODO: interactedTiles(?? Funkar rätt?? Venne)

            Point tiledPosition = GetTiledPositionToPoint();

            switch (facingDirection)
            {
                case MovementDirection.up:
                    if (MapHandler.map[tiledPosition.Y - 1][tiledPosition.X].Interact())
                    {
                        interactedTiles.AddLast(new Point(tiledPosition.X, tiledPosition.Y - 1));
                        MapHandler.UpdateLightmap();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                //break;
                case MovementDirection.down:
                    if (MapHandler.map[tiledPosition.Y + 1][tiledPosition.X].Interact())
                    {
                        interactedTiles.AddLast(new Point(tiledPosition.X, tiledPosition.Y + 1));
                        MapHandler.UpdateLightmap();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                //break;
                case MovementDirection.left:
                    if (MapHandler.map[tiledPosition.Y][tiledPosition.X - 1].Interact())
                    {
                        interactedTiles.AddLast(new Point(tiledPosition.X - 1, tiledPosition.Y));
                        MapHandler.UpdateLightmap();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                //break;
                case MovementDirection.right:
                    if (MapHandler.map[tiledPosition.Y][tiledPosition.X + 1].Interact())
                    {
                        interactedTiles.AddLast(new Point(tiledPosition.X + 1, tiledPosition.Y));
                        MapHandler.UpdateLightmap();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                //break;
                default:
                    return false;
                //break;
            }
        }

        /// <summary>
        /// Gets the facing thile
        /// </summary>
        /// <param name="Map">the Map containing the current map</param>
        /// <returns>returns the facing tile</returns>
        public static Tile GetFacingTile()
        {
            Point tiledPosition = GetTiledPositionToPoint();

            switch (facingDirection)
            {
                case MovementDirection.up:
                    return MapHandler.map[tiledPosition.Y - 1][tiledPosition.X];
                case MovementDirection.down:
                    return MapHandler.map[tiledPosition.Y + 1][tiledPosition.X];
                case MovementDirection.left:
                    return MapHandler.map[tiledPosition.Y][tiledPosition.X - 1];
                case MovementDirection.right:
                    return MapHandler.map[tiledPosition.Y][tiledPosition.X + 1];
                default:
                    return MapHandler.map[0][0];
            }
        }

        public static Point[] GetInteractedTiles()
        {
            //NET-TODO: interactedTiles(?? Funkar rätt?? Venne)

            Point[] interactedTilePointArray = interactedTiles.ToArray();
            interactedTiles.Clear();
            return interactedTilePointArray;
        }

        /// <summary>
        /// Gets the position of the tile the player is facing
        /// </summary>
        /// <returns></returns>
        public static Point GetFacingPositionAsPoint()
        {
            Point tiledPosition = GetTiledPositionToPoint();

            switch (facingDirection)
            {
                case MovementDirection.up:
                    return new Point(tiledPosition.X, tiledPosition.Y - 1);
                case MovementDirection.down:
                    return new Point(tiledPosition.X, tiledPosition.Y + 1);
                case MovementDirection.left:
                    return new Point(tiledPosition.X - 1, tiledPosition.Y);
                case MovementDirection.right:
                    return new Point(tiledPosition.X + 1, tiledPosition.Y);
                default:
                    return Point.Zero;
            }
        }

        /// <summary>
        /// Gets the position of the tile the player is facing
        /// </summary>
        /// <returns></returns>
        public static Vector2 GetFacingPositionAsVector2()
        {
            Point tiledPosition = GetTiledPositionToPoint();

            switch (facingDirection)
            {
                case MovementDirection.up:
                    return new Vector2(tiledPosition.X, tiledPosition.Y - 1);
                case MovementDirection.down:
                    return new Vector2(tiledPosition.X, tiledPosition.Y + 1);
                case MovementDirection.left:
                    return new Vector2(tiledPosition.X - 1, tiledPosition.Y);
                case MovementDirection.right:
                    return new Vector2(tiledPosition.X + 1, tiledPosition.Y);
                default:
                    return Vector2.Zero;
            }
        }

        /// <summary>
        /// Draw the character
        /// </summary>
        /// <param name="spriteBatch">The sprite batch</param> 
        public static void DrawCharacter(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spritesheet, position, drawRectangle, Color.White);

            spriteBatch.DrawString(Global.BasicFont, Global.ToTilePosition(Camera.position).ToString(), new Vector2(0, 80), Color.White);
        }

        /// <summary>
        /// Equips an item from the player's inventory
        /// </summary>
        /// <param name="item">item to equip</param>
        public static void EquipItem(Item item)
        {
            equipsInventory.AddItem(item);
            inventory.RemoveItemDiscreetly(item);

            agility += item.getAgility;
            strength += item.getStrength;

            HUD.UpdateStats();
        }

        public static void UnequipItem(Item item)
        {
            if (inventory.AddItem(item))
            {
                equipsInventory.RemoveItem(item);

                agility -= item.getAgility;
                strength -= item.getStrength;

                HUD.UpdateStats();
            }
        }

        internal static void DrinkItem(Item item)
        {
            switch (item.getType)
            {
                case ItemTypes.HealthPotion:
                    HealPlayer(((HealthPotion)item).HealthRestored);
                    HUD.UpdateHealth();
                    break;
                case ItemTypes.ManaPotion:
                    RestoreMana(((ManaPotion)item).ManaRestored);
                    HUD.UpdateMana();
                    break;
            }

            inventory.RemoveItem(item);
        }
    }
}